package com.lxb.myforum.controller.admin.sysmgt

import javax.servlet.http.HttpServletRequest

import com.lxb.myforum.base.BaseController
import com.lxb.myforum.domain.ResResult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody

import com.lxb.myforum.domain.PageResponse
import com.lxb.myforum.pojo.Resource
import com.lxb.myforum.service.ResourceService

/**
 *
 * @author lxb
 */
@Controller("adminResourceCtrl")
@RequestMapping("/admin/resources")
class ResourceController @Autowired
constructor(private val resourceService: ResourceService) : BaseController() {

    @GetMapping(value = ["/", ""])
    fun index(): String {
        return "/admin/sysmgt/resources/index"
    }

    @GetMapping(value = ["/list", "/all"])
    @ResponseBody
    fun resources(page: Int, size: Int): PageResponse<Resource> {
        val pageable = PageRequest.of(page - 1, size)
        val users = resourceService.getResources(pageable)
        return PageResponse(0L, "", totalCount = users.totalElements, rows = users.content)
    }

    @RequestMapping(value = ["", "/"], method = [RequestMethod.PUT, RequestMethod.POST])
    @ResponseBody
    fun saveRes(req: HttpServletRequest, @ModelAttribute resource: Resource): Resource {
        return resourceService.saveRes(resource)
    }

    @DeleteMapping(value = ["/{id}"])
    @ResponseBody
    fun delRes(@PathVariable("id") id: Long): ResResult {
        return resourceService.deleteResById(id)
    }
}
